# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymongo
from mafengwo import items

class MafengwoPipeline(object):
    def __init__(self):
        self.serverIP = '127.0.0.1'
        self.serverPort = 54545
        self.serverDB = 'mafengwo'
        self.client = pymongo.MongoClient(self.serverIP, self.serverPort)
        self.db = self.client[self.serverDB]
        self.mdd = self.db['mdd']
        self.youji = self.db['youji']
        self.jd = self.db['jd']
        self.hotel = self.db['hotel']
        self.res = self.db['res']


    def addMDD(self, mdd):
        if self.mdd.find_one({'cname': mdd['cname'], 'url': mdd['url']}):
            return
        self.mdd.save(dict(mdd))
    
    def addMayouji(self, youji):
        if self.youji.find_one({'url': youji['url']}):
            return
        self.youji.save(dict(youji))
    
    def addMaJD(self, jd):
        if self.jd.find_one({'url':jd['url']}):
            return
        self.jd.save(dict(jd))
    
    def addMaHotel(self, hotel):
        if self.hotel.find_one({'url': hotel['url']}):
            return
        self.hotel.save(dict(hotel))

    def addMaRes(self, res):
        if self.res.find_one({'url': res['url']}):
            return
        self.res.save(dict(res))

    def process_item(self, item, spider):
        if isinstance(item, items.MaMDD):
            self.addMDD(item)
        elif isinstance(item, items.MaYouji):
            self.addMayouji(item)
        elif isinstance(item, items.MaJD):
            self.addMaJD(item)
        elif isinstance(item, items.MaHotel):
            self.addMaHotel(item)
        elif isinstance(item, items.MaRes):
            self.addMaRes(item)
        else:
            print "can not deal the item ", item
